package com.kq.tallybookl.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.kq.tallybookl.R;
import com.kq.tallybookl.adapter.ItemAdapter;
import com.kq.tallybookl.database.FakeManager;
import com.kq.tallybookl.database.TrueManager;
import com.kq.tallybookl.item.MenoItem;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 目录显示界面
 *
 * Created by Kq on 2016/4/30.
 */
public class TrueActivity extends Activity{

    private ArrayList<MenoItem> list;
    private ItemAdapter adapter;

    private TextView textView_title;
    private Button button_add;
    private Button button_set;
    private Button button_selectAll;
    private Button button_cancel;
    private Button button_delect;
    private ListView listView;

    private TrueManager trueManager;
    private FakeManager fakeManager;

    private boolean verificationStatus;

    private int selectSum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_true);

        checkStatus();

        loadData();

        initButton();

        initListView();

        textView_title = (TextView) findViewById(R.id.true_textView_title);
    }

    /**
     * 初始化ListView
     */
    private void initListView() {
        listView = (ListView) findViewById(R.id.true_listView);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(adapter.isSelectStatus()){
                    // 更改item选择状态
                    list.get(position).setStatus(!list.get(position).isStatus());
                    if(list.get(position).isStatus()) {
                        selectSum += 1;
                    }else {
                        selectSum -= 1;
                    }
                    // 刷新ListView
                    adapter.notifyDataSetChanged();
                    changeTitle();
                }else{
                    // 进入编辑界面
                    Intent gotoEditActivity = new Intent(TrueActivity.this,EditActivity.class);
                    gotoEditActivity.putExtra(PreferencesActivity.VERIFICATION_STATUS,verificationStatus);
                    gotoEditActivity.putExtra(PreferencesActivity.MENOITEM_TITLE,list.get(position).getTitle());
                    gotoEditActivity.putExtra(PreferencesActivity.MENOITEM_TIME,list.get(position).getTimestamp());
                    gotoEditActivity.putExtra(PreferencesActivity.MENOITEM_DETAILS,list.get(position).getDetails());
                    startActivity(gotoEditActivity);
                }
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                // 改变ListView可选择状态
                if(adapter.isSelectStatus()){
                    list.get(position).setStatus(!list.get(position).isStatus());
                    if(list.get(position).isStatus()) {
                        selectSum += 1;
                    }else {
                        selectSum -= 1;
                    }
                    adapter.notifyDataSetChanged();
                }else{
                    list.get(position).setStatus(!list.get(position).isStatus());
                    selectSum =1;
                    adapter.changeStatus(true);
                    adapter.notifyDataSetChanged();
                    viewDowmVisible(button_cancel);
                    viewDowmVisible(button_selectAll);
                    viewDownGone(button_add);
                    viewUpVisible(button_delect);

                    changeTitle();
                }

                return false;
            }
        });
    }

    /**
     * 改变头部文字信息
     */
    private void changeTitle() {
        if(adapter.isSelectStatus()){
            if(selectSum==0){
                textView_title.setText("请选择项目");
            }else {
                textView_title.setText("已选择"+selectSum+"项");
            }
        }else {
            textView_title.setText("记事本");
        }
    }

    /**
     * 将View下滑可见
     * @param valueView
     */
    private void viewDowmVisible(View valueView) {
        TranslateAnimation slideDowm = new TranslateAnimation(0,0,-100,0);
        slideDowm.setInterpolator(new AccelerateInterpolator());
        slideDowm.setDuration(500);
        final View view = valueView;
        slideDowm.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(slideDowm);
    }

    /**
     * 将View上滑可见
     * @param valueView
     */
    private void viewUpVisible(View valueView) {
        TranslateAnimation slideDowm = new TranslateAnimation(0,0,100,0);
        slideDowm.setInterpolator(new AccelerateInterpolator());
        slideDowm.setDuration(500);
        final View view = valueView;
        slideDowm.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(slideDowm);
    }

    /**
     * 将View下滑不可见
     * @param valueView
     */
    private void viewDownGone(View valueView) {
        TranslateAnimation slideDowm = new TranslateAnimation(0,0,0,100);
        slideDowm.setInterpolator(new AccelerateInterpolator());
        slideDowm.setDuration(500);
        final View view = valueView;
        slideDowm.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        view.startAnimation(slideDowm);
    }

    /**
     * 将View上滑不可见
     * @param valueView
     */
    private void viewUpGone(View valueView) {
        TranslateAnimation slideDowm = new TranslateAnimation(0,0,0,-100);
        slideDowm.setInterpolator(new AccelerateInterpolator());
        slideDowm.setDuration(500);
        final View view = valueView;
        slideDowm.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        view.startAnimation(slideDowm);
    }

    /**
     * 初始化按钮
     */
    private void initButton() {
        button_add = (Button) findViewById(R.id.true_button_add);
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入编辑界面
                Intent gotoEditActivity = new Intent(TrueActivity.this,EditActivity.class);
                gotoEditActivity.putExtra(PreferencesActivity.VERIFICATION_STATUS,verificationStatus);
                startActivity(gotoEditActivity);
            }
        });

        button_set = (Button) findViewById(R.id.true_button_set);
        button_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入设置界面
                Intent gotoSetting = new Intent(TrueActivity.this,SettingActivity.class);
                gotoSetting.putExtra(PreferencesActivity.VERIFICATION_STATUS,verificationStatus);
                startActivity(gotoSetting);
            }
        });

        button_cancel = (Button) findViewById(R.id.true_button_cancel);
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelSelect();
            }
        });

        button_selectAll = (Button) findViewById(R.id.true_button_selectAll);
        button_selectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(MenoItem item : list){
                    item.setStatus(true);
                }
                selectSum = list.size();
                adapter.notifyDataSetChanged();
                changeTitle();
            }
        });

        button_delect = (Button) findViewById(R.id.true_button_delete);
        button_delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                for(MenoItem item : list){
//                    if(item.isStatus()){
//                        list.remove(item);
//                        if(verificationStatus){
//                            trueManager.deleteMenoItem(item);
//                        }else {
//                            fakeManager.deleteMenoItem(item);
//                        }
//                    }
//                }
                Iterator<MenoItem> iterator = list.iterator();
                while (iterator.hasNext()){
                    MenoItem item = iterator.next();
                    if(item.isStatus()){
                        if(verificationStatus){
                            trueManager.deleteMenoItem(item);
                        }else {
                            fakeManager.deleteMenoItem(item);
                        }
                        iterator.remove();
                    }
                }
                cancelSelect();
            }
        });
    }

    /**
     * 取消选择
     */
    private void cancelSelect() {
        if(adapter.isSelectStatus()){
            adapter.changeStatus(false);
            for(MenoItem item : list){
                item.setStatus(false);
            }
            adapter.notifyDataSetChanged();

            viewUpGone(button_cancel);
            viewUpGone(button_selectAll);
            viewUpVisible(button_add);
            viewDownGone(button_delect);

            selectSum = 0;
            changeTitle();
        }
    }

    /**
     * 加载数据
     */
    private void loadData() {
        fakeManager = new FakeManager(this);
        if(verificationStatus) {
            String key = fakeManager.getLoginPassword();
            trueManager = new TrueManager(this, key);
            list = (ArrayList<MenoItem>) trueManager.buildMenoItems();
        }else {
            list = (ArrayList<MenoItem>) fakeManager.buildMenoItems();
        }
        // 如果数据为空，则显示默认信息
        if(list.isEmpty()){
            MenoItem simpleItem = new MenoItem("欢迎使用记事本",System.currentTimeMillis(),"");
            list.add(simpleItem);
            if(verificationStatus){
                trueManager.addMenoItem(simpleItem);
            }
        }
        adapter = new ItemAdapter(list,this);

        selectSum = 0;
    }

    /**
     * 获取传入的状态标示
     */
    private void checkStatus() {
        verificationStatus = false;
        final Intent intent = this.getIntent();
        if(intent == null){
            finish();
        }else{
            verificationStatus = intent.getBooleanExtra(PreferencesActivity.VERIFICATION_STATUS,false);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}
